package com.example.fresh_fruit.controller;

import com.example.fresh_fruit.dto.OrderDto;
import com.example.fresh_fruit.entity.Commodity;
import com.example.fresh_fruit.entity.OrderDetails;
import com.example.fresh_fruit.service.CommodityService;
import com.example.fresh_fruit.service.HeadService;
import com.example.fresh_fruit.service.OrderDtoService;
import com.example.fresh_fruit.util.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :飞天の鱼
 * @date : 2022/8/23 9:19
 */
@Slf4j
@RestController
@RequestMapping("/orderDtoController")
public class OrderDtoController {
    @Autowired
    private OrderDtoService orderDtoService;

    @Autowired
    private HeadService headService;

    @Autowired
    private CommodityService commodityService;


    @PostMapping("/insert")
    @ApiOperation(value = "插入订单")
    public Result insert(@RequestBody OrderDto orderDto) {
        Integer checkHead = headService.checkHead(orderDto.getHeadId());
        if (checkHead != 0) {
            //log.info("团长方面出现问题");
            return Result.fail("团长方面出现问题");
        }
        List<OrderDetails> detailsList = orderDto.getOrderDetailsList();
        for (OrderDetails orderDetail : detailsList) {
            Integer commodityId = orderDetail.getCommodityId();
            int checkCommodity = commodityService.checkCommodity(commodityId);
            //检查商品的各个状态
            if (checkCommodity == 0) {
                //订单需求的数量
                double requireNumber = orderDetail.getRequireNumber();

                //商品能给的数量
                Commodity commodity = commodityService.findById(commodityId);
                double comNumber=commodity.getNumber();
                //商品能给的数量-->需求的数量
                if (comNumber < requireNumber) {
                    //商品名称以及数量

                    //log.info("name:{},number:{}", commodity.getName(), commodity.getNumber());

                    return Result.fail("商品数量出错");
                } else {
                    //计算订单总金额
                    double price = orderDetail.getPrice();
                    double number = orderDetail.getRequireNumber();
                    double orderMoney=price*number;
                    orderDto.setOrderMoney(orderMoney);
                    //修改下订单后的数量
                    double comNumber1=comNumber-requireNumber;
                    commodity.setNumber(comNumber1);
                    commodityService.updata(commodity);
                    //检查完团长以及商品后，开始插入两表的数据
                    int insertOrder = orderDtoService.insertOrder(orderDto);

                    if (insertOrder == 0) {
                        return Result.ok("成功");
                    }
                }


            } else if (checkCommodity!=0){

                return Result.fail("商品出错");
            }
        }
        return Result.ok("成功");
    }

}
